package com.onjob.security;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;



import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private  Users user ;
    private  GroupsDao groupdo ;
    private  UsersDao userdo;
    private  String message;
    private  String userName;
    private  String userPass;
    private String target;
    public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public GroupsDao getGroupdo() {
		return groupdo;
	}

	public void setGroupdo(GroupsDao groupdo) {
		this.groupdo = groupdo;
	}

	public UsersDao getUserdo() {
		return userdo;
	}

	public void setUserdo(UsersDao userdo) {
		this.userdo = userdo;
	}
	
	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}
	 public String ckeck() throws Exception{
		 System.err.println("username "+this.getUserName() );
		 System.err.println("userpass "+this.getUserPass());
		 Users users = new Users();
		 users.setUserName(userName);
		 users.setUserPassword(userPass);
		 this.user = this.userdo.findByUsers(users);
		 if(this.user!=null){
		     ServletActionContext.getRequest().getSession().setAttribute("user", this.user);
		 }
		 else{
			 this.message="用户名或密码不正确";
			
		 }
		 return "success";
		    //System.err.println( ServletActionContext.getRequest().getSession().getAttribute("user"));
		  
	   }
	  public String login() throws Exception{
		   if( (String)ServletActionContext.getRequest().getSession().getAttribute("message")!=null){
		     this.setMessage( (String)ServletActionContext.getRequest().getSession().getAttribute("message"));
		   }
		   else{
			   this.message=null;
		   }
		   return "input";
	   }
    public String logindo()throws Exception{
        System.err.println("您已经成功登录");
        //System.err.println(user.getUserName());
        Users users = this.userdo.findByUsers(user);
       
        if(users!=null){
        	ServletActionContext.getRequest().getSession().setAttribute("user", users);
        	Long var = null;
        	List<String> list = new ArrayList<String>();
        	list=this.groupdo.findAuthoritys(users) ;
        	Iterator<Groups> it=users.getGroupsId().iterator();
        	while(it.hasNext()){
        		var=it.next().getId();
        		System.out.println(var);
        	}
       	if(var.intValue()==1){
        		System.out.println("个人主页入口");
        		this.setTarget("login");
        	}
        	else if(var.intValue()==2){
        		System.out.println("企业主页入口");
        		this.setTarget("registerPersonInfo");
        	}
        	else if(var.intValue()==3){
        		System.out.println("管理员主页入口");
        			this.setTarget("enter");
        	}
        	Groups.getUrlMap().put(var, list);
        	System.err.println("UrlMap==================="+Groups.getUrlMap().size());
        	return "success";
        }
        else{
        	 this.setMessage("用户名或密码不正确！");
        	 return "input";
        }
     
     	
     }
    public String exit()throws Exception{
    	ServletActionContext.getRequest().getSession().removeAttribute("user");
  	  return "success";
    }

}
